package com.bdsys.action;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.bdsys.common.action.BasicAction;
import com.bdsys.common.constants.Constants;
import com.bdsys.form.Device;
import com.bdsys.service.DeviceService;
import com.bdsys.servicedto.DeviceDetailDTO;
import com.bdsys.servicedto.DeviceServiceDTO;
import com.bdsys.vo.DeviceVo;

@SuppressWarnings("serial")
public class DeviceAction extends BasicAction implements Constants{
	
	Device device;
	
	private DeviceService  deviceService = (DeviceService)getService(DEVICE_SERVICE);
	
	public String add(){
		deviceService.addDevice(this.createServiceParameter());
		return EDITSUCCESS;
	}
	
	public String selectByConditions(){
		DeviceServiceDTO outServiceDTO = deviceService.selectByConditions(this.createServiceParameter());
		
		List<DeviceDetailDTO> outList = outServiceDTO.getList();
		
		List<DeviceVo> viewList = new ArrayList<DeviceVo>();
		for (DeviceDetailDTO outDetailDTO : outList) {
			DeviceVo deviceVo = this.fromDetailDTOToVo(outDetailDTO);
			viewList.add(deviceVo);
		}
		
		if(device != null){
			if("forUpdate".equals(device.getAction())){
				DeviceVo deviceVo = viewList.get(0);
				getRequest().put("deviceVo", deviceVo);
				getRequest().put("isUpdate", "yes");
				this.setPage("/device/editDevice.jsp");
				getRequest().put("updateItem-timeType", "0".equals(deviceVo.getTimeType()) ? "00" : deviceVo.getTimeType());
				getRequest().put("updateItem-available", "0".equals(deviceVo.getAvailable()) ? "00" : deviceVo.getAvailable());
			}else{
				getRequest().put("deviceList", viewList);
				this.setPage("/device/deviceList.jsp");
				getRequest().put("condition-timeType", "0".equals(device.getTimeType()) ? "00" : device.getTimeType());
				getRequest().put("condition-available", "0".equals(device.getAvailable()) ? "00" : device.getAvailable());
			}
			
		}else {
			getRequest().put("deviceList", viewList);
			this.setPage("/device/deviceList.jsp");
		}
		return SELECTBYCONDITIONS;
	}
	
	public String delete(){
		deviceService.deleteDevice(this.createServiceParameter());
		getRequest().put("isDelete", "yes");
		return EDITSUCCESS;
	}
	
	public String update(){
		deviceService.updateDevice(this.createServiceParameter());
		getRequest().put("isUpdate", "yes");
		return EDITSUCCESS;
	}
	
	private DeviceDetailDTO fromFromToDetailDTO(Device device){
		device = (device == null ? new Device() : device);
		DeviceDetailDTO detailDTO = new DeviceDetailDTO();
		detailDTO.setCode(device.getCode());
		detailDTO.setName(device.getName());
		
		File[] images = device.getImageFile();
		String [] imageNames = device.getImageFileName();
		String [] imageDecription = device.getImageDescription();
		Map<String, byte[]> idFile = new HashMap<String,byte[]>();
		Map<String, String> idDescription = new HashMap<String,String>();
		Map<String, String> idName = new HashMap<String,String>();
		if (images != null) {
			for (int i = 0; i < images.length; i++) {
				File image = images[i];
				byte[] buffer = null;
				FileInputStream fis;
				try {
					fis = new FileInputStream(image);
					ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);
					byte[] b = new byte[1000];
					int n;
					while ((n = fis.read(b)) != -1) {
						bos.write(b, 0, n);
					}
					fis.close();
					bos.close();
					buffer = bos.toByteArray();
					if(buffer != null){
						idFile.put(String.valueOf(i), buffer);
					}
				} catch (Exception e) {
					e.printStackTrace();
				}

				if (imageNames != null && imageNames.length > i && !StringUtils.isEmpty(imageNames[i])) { 
					idName.put(String.valueOf(i), imageNames[i].trim());
					if (imageDecription != null && imageDecription.length > i && !StringUtils.isEmpty(imageDecription[i])) {
						idDescription.put(String.valueOf(i), imageDecription[i]);
					} else {
						idDescription.put(String.valueOf(i), "noDescription");
					}
				} else {
					idName.put(String.valueOf(i), "noName");

					if (imageDecription != null && imageDecription.length > i && !StringUtils.isEmpty(imageDecription[i])) {
						idDescription.put(String.valueOf(i), imageDecription[i]);
					} else {
						idDescription.put(String.valueOf(i), "noDescription");
					}
				}
			}
		}
		
		if(device.getIsShowImage() != null){
			String[] isShowImage = device.getIsShowImage().split(",");
			Map<String, String> idIsShowImage = new HashMap<String, String>();
			idIsShowImage.put(isShowImage[1], isShowImage[0]);
			detailDTO.setIdIsShowImage(idIsShowImage);
		}
		
		detailDTO.setIdFile(idFile);
		detailDTO.setIdName(idName);
		detailDTO.setIdDescription(idDescription);
		detailDTO.setTimeType(device.getTimeType());
		detailDTO.setAvailable(device.getAvailable());
		detailDTO.setDescription(device.getDescription());
		
		return detailDTO;
	}
	
	private DeviceVo fromDetailDTOToVo(DeviceDetailDTO detailDTO){
		DeviceVo deviceVo = new DeviceVo();
		deviceVo.setCode(detailDTO.getCode());
		deviceVo.setName(detailDTO.getName());
		deviceVo.setTimeType(detailDTO.getTimeType());
		deviceVo.setAvailable(detailDTO.getAvailable());
		deviceVo.setDescription(detailDTO.getDescription());
		return deviceVo;
	}

	public void setDevice(Device device){
		this.device = device;
	}
	
	public Device getDevice(){
		return this.device;
	}
	
	private DeviceServiceDTO createServiceParameter(){
		DeviceDetailDTO inDetailDTO = this.fromFromToDetailDTO(device);
		DeviceServiceDTO inServiceDTO = new DeviceServiceDTO();
		inServiceDTO.setDeviceDetailDTO(inDetailDTO);
		return inServiceDTO;
	}
	
	public String showImage(){
		FileInputStream fis;
		try {
			OutputStream out = getResponse().getOutputStream();
			fis = new FileInputStream(device.getImageFile()[0]);
			byte[] b = new byte[1024];
			fis.read(b);

			for (int i = 0; i < b.length; i++) {
				out.write(b[i]);
			}
			fis.close();
			out.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		this.setPage("/device/editDevice.jsp");
		return SELECTBYCONDITIONS;
	}
}
